package com.seven.ezpermissions.helper

import androidx.appcompat.app.AlertDialog
import com.seven.ezpermissions.OnDeniedCallback

/**
 * 说明
 * @author seven.xie
 * @date 2020/7/10 14:42
 * email: se7en.xie@qq.com
 */
abstract class BasePermissionsHelper<T> constructor(host: T) : PermissionsHelper<T>(host) {

    private var rationaleDialog: AlertDialog? = null

    override fun showRationale(
        rationale: String,
        positiveButton: String,
        negativeButton: String,
        requestCode: Int,
        permissions: Array<out String>,
        onDeniedCallback: OnDeniedCallback?
    ) {
        val context = context ?: return
        rationaleDialog = AlertDialog.Builder(context)
            .setCancelable(false)
            .setMessage(rationale)
            .setPositiveButton(positiveButton) { dialog, _ ->
                dialog.dismiss()
                // 接受解释，直接请求权限
                directRequest(requestCode, permissions)
            }
            .setNegativeButton(negativeButton) { dialog, _ ->
                dialog.dismiss()
                onDeniedCallback?.invoke(requestCode, permissions.asList())
            }
            .show()
    }

    override fun onClear() {
        rationaleDialog?.dismiss()
        rationaleDialog = null
        super.onClear()
    }
}